package com.wx.service.impl;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wx.common.utils.ImagePathUtil;
import com.wx.mapper.SourceMapper;
import com.wx.pojo.IndexSource;
import com.wx.pojo.Source;
import com.wx.service.SourceService;
@Service
public class SourceServiceImpl implements SourceService {

	@Autowired
	private SourceMapper sourceMapper;
	
	@Override
	public List<IndexSource> findSourceAll() {
		List<IndexSource> sourceList=sourceMapper.findSourceAll();
		if(!sourceList.isEmpty() || sourceList.size()>0) {
			for (IndexSource source : sourceList) {
				if(source.getSource_image()!=null || source.getSource_image()!="") {
					
//					String path=source.getSource_image();
//					if(path.contains(",")) {
//						int first= path.indexOf(",");
//						path=path.substring(0, first);
//					}
					source.setSource_image(ImagePathUtil.firstSourcePath(source.getSource_image()));
				}
			}
		}
		return sourceList;
	}

	@Override
	public List<IndexSource> findSourceRecent(Date date) {
		List<IndexSource> sourceList=sourceMapper.findSourceRecent(date);
		if(!sourceList.isEmpty() || sourceList.size()>0) {
			for (IndexSource source : sourceList) {
				if(source.getSource_image()!=null || source.getSource_image()!="") {
					
//					String path=source.getSource_image();
//					if(path.contains(",")) {
//						int first= path.indexOf(",");
//						path=path.substring(0, first);
//					}
					source.setSource_image(ImagePathUtil.firstSourcePath(source.getSource_image()));
				}
			}
		}
		
		return sourceList;
	}

	@Override
	public List<IndexSource> findSourceTop() {
		List<IndexSource> sourceList=sourceMapper.findSourceTop();
		if(!sourceList.isEmpty() || sourceList.size()>0) {
			for (IndexSource source : sourceList) {
				if(source.getSource_image()!=null || source.getSource_image()!="") {
					
					String path=source.getSource_image();
//					if(path.contains(",")) {
//						int first= path.indexOf(",");
//						path=path.substring(0, first);
//					}
					source.setSource_image(ImagePathUtil.firstSourcePath(path));
				}
			}
		}
		return sourceList;
		
	}

	@Override
	public List<IndexSource> findSourceByCategory(Integer categoryId) {
		List<IndexSource> sourceCategotyList=sourceMapper.findSourceByCategory(categoryId);
		
		if(!sourceCategotyList.isEmpty() || sourceCategotyList.size()>0) {
			for (IndexSource source : sourceCategotyList) {
				if(source.getSource_image()!=null || source.getSource_image()!="") {
					
					String path=source.getSource_image();
					source.setSource_image(ImagePathUtil.firstSourcePath(path));
				}
			}
		}
		
		return sourceCategotyList;
	}

	/**
	 * 查询商品信息通过名称查询
	 */
	@Override
	public List<IndexSource> findSourceByName(String sourceName) {
		List<IndexSource>sourceList=sourceMapper.findSourceByName(sourceName);
		System.out.println(sourceList);
		if(!sourceList.isEmpty() || sourceList.size()>0) {
			for (IndexSource source : sourceList) {
				if(source.getSource_image()!=null || source.getSource_image()!="") {
					
					String path=source.getSource_image();
					source.setSource_image(ImagePathUtil.firstSourcePath(path));
				}
			}
		}
		
		return sourceList;
	}

}
